package com.littleHan.brushQuestions.web.uploadTopic.controller;

import com.alibaba.fastjson.JSON;
import com.littleHan.brushQuestions.common.Result;
import com.littleHan.brushQuestions.exception.FailedToInsertException;
import com.littleHan.brushQuestions.exception.FailedToLoadException;
import com.littleHan.brushQuestions.utils.GetTokenInfoUtil;
import com.littleHan.brushQuestions.web.start.pojo.Topic;
import com.littleHan.brushQuestions.web.uploadTopic.service.UploadTopicService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@RestController
@RequestMapping("/uploadTopic")
public class UploadTopicController {

    @Autowired
    private UploadTopicService uploadTopicService;

    /**
     * 获取题目类型学科以及难度
     * @return 获取题目类型学科以及难度是否成功
     * @throws FailedToLoadException
     */
    @RequestMapping(value = "/getTopicAboutJ",method = RequestMethod.POST)
    public String getTopicAbout() throws FailedToLoadException {
        Map<String,Object> map=uploadTopicService.getTopicAbout();
        return JSON.toJSONString(new Result().setCode(200).setMessage("获取题目类型学科难度列表失败").setData(map));
    }

    /**
     * 添加题目
     * @param topic 题目实例
     * @return 添加题目是否成功
     */
    @RequestMapping(value = "/goUploadTopicJ",method = RequestMethod.POST)
    public String goUploadTopic(@RequestBody Topic topic, HttpServletRequest request) throws FailedToInsertException {
        String userId= GetTokenInfoUtil.getUserId(request);
        uploadTopicService.goUploadTopic(topic,userId);
        return JSON.toJSONString(new Result().setCode(200).setMessage("添加题目成功"));
    }
}
